package com.example.Project.user;

import com.example.Project.Class.User;
import com.example.Project.user.Methods.ToExcel;
import com.example.Project.user.userHttpMethods.userMethods;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class ShowMassUserController {
    private static int id;
    public ContextMenu menu1;

    void start() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("showMassUser.fxml"));
        Scene scene = new Scene((Parent)fxmlLoader.load(), 630.0, 439.0);
        Stage stage=new Stage();
        stage.setTitle("社团信息管理");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }
    void get(int theId){
        id=theId;
        System.out.println(id);
    }
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<User> table;

    @FXML
    private TableColumn<User,Integer > tableId;

    @FXML
    private TableColumn<User, String> tableName;

    @FXML
    private TableColumn<User, String > tableSex;

    @FXML
    private TableColumn<User, String > tablePhone;

    @FXML
    private Button toExcelButton;

    @FXML
    void ToExcel(ActionEvent event) {
        ObservableList<User> tableData = table.getItems();
        List<User> massToExcel = new ArrayList<>(tableData);
        ToExcel.setInfo(massToExcel);
    }
    com.example.Project.user.userHttpMethods.userMethods userMethods=new userMethods();
    @FXML
    public void initialize() throws ClassNotFoundException, IOException {
        table.getItems().clear();
        ObservableList<User> userList= userMethods.getMassUser(id);
        System.out.println(userList);
        Platform.runLater(() -> updateTableView(userList));
    }
    private void updateTableView(List<User> UserList) {
        this.table.getItems().addAll(UserList);
        this.tableId.setCellValueFactory(new PropertyValueFactory<>("id"));
        this.tableName.setCellValueFactory(new PropertyValueFactory<>("name"));
        this.tableSex.setCellValueFactory(new PropertyValueFactory<>("sex"));
        this.tablePhone.setCellValueFactory(new PropertyValueFactory<>("phone"));
    }

    public void btnDel(ActionEvent actionEvent) throws IOException {
        boolean re=userMethods.deleteMassUser(table.getSelectionModel().getSelectedItem().getId(),id);
        Alert al = new Alert(Alert.AlertType.CONFIRMATION);
        if(re){
            al.setHeaderText("删除成员成功！！！");
        }else {
            al.setHeaderText("操作失败，请重新进行操作！！！");
        }
        al.showAndWait();
        table.getItems().clear();
        ObservableList<User> UserList2=userMethods.getMassUser(id);
        if(UserList2!=null) {
            Platform.runLater(() -> updateTableView(UserList2));
        }
    }
}
